import 'package:flutter_admin/models/odoo/mps_demand_line.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class MpsDemandLineDialog extends StatefulWidget {
  MpsDemandLineDialog();

  @override
  _MpsDemandLineDialogState createState() => _MpsDemandLineDialogState();
}

class _MpsDemandLineDialogState extends State<MpsDemandLineDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  DateFormat format = DateFormat('yyyy-MM-dd');

  MpsDemandLine mpsDemandLine = MpsDemandLine();

  @override
  void initState() {
    super.initState();
    mpsDemandLine.dateDemandStart = Utils.getDate();
    mpsDemandLine.dateDemandEnd = Utils.getDate();
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelectDate(
            context,
            width: 280,
            label: '交货开始时间',
            value: mpsDemandLine.dateDemandStart,
            lastDate: isNotBlank(mpsDemandLine.dateDemandEnd)
                ? format.parse(mpsDemandLine.dateDemandEnd!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              mpsDemandLine.dateDemandStart = v;
              setState(() {});
            },
            onSaved: (v) {
              mpsDemandLine.dateDemandStart = v;
            },
          ),
          CrySelectDate(
            context,
            width: 280,
            label: '交货结束时间',
            value: mpsDemandLine.dateDemandEnd,
            firstDate: isNotBlank(mpsDemandLine.dateDemandStart)
                ? format.parse(mpsDemandLine.dateDemandStart!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              mpsDemandLine.dateDemandEnd = v;
              setState(() {});
            },
            onSaved: (v) {
              mpsDemandLine.dateDemandEnd = v;
            },
          ),
          CryInput(
            width: 280,
            label: '订单号',
            value: mpsDemandLine.origin,
            onSaved: (v) {
              mpsDemandLine.origin = v;
            },
          ),
          CryInput(
            width: 280,
            label: '产品名称',
            value: mpsDemandLine.productName,
            onSaved: (v) {
              mpsDemandLine.productName = v;
            },
          ),
          CryInput(
            width: 280,
            label: '产品编码',
            value: mpsDemandLine.defaultCode,
            onSaved: (v) {
              mpsDemandLine.defaultCode = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, mpsDemandLine);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
